package com.runfig.work.config;

import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.JobTypeConfiguration;
import com.dangdang.ddframe.job.config.dataflow.DataflowJobConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.event.JobEventConfiguration;
import com.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.runfig.work.job.FileDataflowJob;
import com.runfig.work.job.MyJob;
import com.runfig.work.job.OneJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;

@Configuration
public class JobConfig {


    @Autowired
    private DataSource dataSource;

    @Autowired
    private CoordinatorRegistryCenter coordinatorRegistryCenter;


    @Bean(initMethod = "init")
    public SpringJobScheduler springJobScheduler(FileDataflowJob job) {

        JobEventConfiguration jobEventConfiguration = new JobEventRdbConfiguration(dataSource);

        return new SpringJobScheduler(job, coordinatorRegistryCenter,
                createJobConfiguration(
                        job.getClass(),
                        "0/3 * * * * ?",
                        4,
                        "0=text,1=image,2=radio,3=vedio",true),jobEventConfiguration);
    }

//    @Bean(initMethod = "init")
//    public SpringJobScheduler springJobScheduler(OneJob job) {
//        return new SpringJobScheduler(job, coordinatorRegistryCenter, createJobConfiguration(job.getClass(), "0/3 * * * * ?"));
//    }

//    @Bean(initMethod = "init")
//    public SpringJobScheduler springJobScheduler(MyJob myJob) {
//        return new SpringJobScheduler(myJob, coordinatorRegistryCenter, createJobConfiguration(myJob.getClass(), "0/3 * * * * ?"));
//    }

    private LiteJobConfiguration createJobConfiguration(Class clz, String cron, Integer shardingCount, String shardingParam, boolean dataflowType) {

        JobCoreConfiguration.Builder builder = JobCoreConfiguration.newBuilder(clz.getSimpleName(), cron, shardingCount);

        if (!StringUtils.isEmpty(shardingParam)) {
            builder.shardingItemParameters(shardingParam);
        }

        JobTypeConfiguration jobConfig = null;
        // 定义作业核心配置
        JobCoreConfiguration simpleCoreConfig = builder.build();
        if (dataflowType) {
            jobConfig = new DataflowJobConfiguration(simpleCoreConfig,clz.getCanonicalName(),true);
        } else {
            // 定义SIMPLE类型配置
            jobConfig = new SimpleJobConfiguration(simpleCoreConfig, clz.getCanonicalName());
        }

        // 定义Lite作业根配置
        LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(jobConfig).overwrite(true).build();
        return simpleJobRootConfig;
    }
}
